Repository Public
1// Vrai-Prix — méthode du coût : fiche d'un article (prix canonique, historique, provenance, assemblages qui l'utilisent).2import { NextRequest, NextResponse } from "next/server";3import { buildContext, getItem, loadAssemblies, priceHistory } from "@/lib/cost/catalog";4import { cached } from "@/lib/cost/cache";56export const runtime = "nodejs";78export async function GET(req: NextRequest, { params }: { params: Promise<{ code: string }> }) {9 const { code } = await params;10 const loc = req.nextUrl.searchParams.get("location") ?? "QC-MTL";11 try {12 const item = getItem(decodeURIComponent(code));13 if (!item) return NextResponse.json({ error: "article introuvable" }, { status: 404 });14 const ctx = cached(`ctx:${loc}`, 10 * 60_000, () => buildContext({ locationCode: loc }));15 const price = ctx.prices.get(item.code) ?? null;16 const history = priceHistory(item.code);17 const usedBy = [...loadAssemblies().values()].filter((a) => a.components.some((c) => c.itemCode === item.code)).map((a) => ({ code: a.code, nameFr: a.nameFr, nameEn: a.nameEn, unit: a.unit, quantity: a.components.find((c) => c.itemCode === item.code)!.quantity }));18 return NextResponse.json({ asOf: ctx.asOf, item, price, history, usedBy });19 } catch (e) {20 return NextResponse.json({ error: (e as Error).message }, { status: 500 });21 }22}23